﻿
$(function(){
	var pathName=window.document.location.pathname;
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
	
	//异步获取题目最大数量
	$('.grade').on('click', function() {
		var checkedGrade = [];
		$(".grade:checked").each(function() {
			var gradeId = $(this).val();
			var grade = {
					"gradeId":gradeId
			}
			checkedGrade.push(grade);
		});
		var questionScopeVal = $(".questionScope:checked").val();
		var urlString = "";
		if(questionScopeVal == "all") {
			urlString = projectName + "/Word/howMuchWordOfGrade";
		} else if (questionScopeVal == "star") {
			urlString = projectName + "/Word/howMuchWordOfGradeAndStar";
		} else if (questionScopeVal == "wrong") {
			urlString = projectName + "/Word/howMuchWordOfGradeAndWrong";
		}
		$.ajax({
			url:urlString,
			type:"post",
			async:true,
			data:JSON.stringify(checkedGrade),
//			dataType:"json",
			contentType:"application/json",
			success:function (data) {
				$("#maxCountTs").text(data);
				var maxQuestionCountVal = $("#maxQuestionCount").val();
				if(data - maxQuestionCountVal < 0) {
					$("#maxQuestionCount").val(data);
				}
			}
		});
	});
	
	$('.questionScope').on('click', function() {
		var checkedGrade = [];
		$(".grade:checked").each(function() {
			var gradeId = $(this).val();
			var grade = {
					"gradeId":gradeId
			}
			checkedGrade.push(grade);
		});
		var questionScopeVal = $(".questionScope:checked").val();
		var urlString = "";
		if(questionScopeVal == "all") {
			urlString = projectName + "/Word/howMuchWordOfGrade";
		} else if (questionScopeVal == "star") {
			urlString = projectName + "/Word/howMuchWordOfGradeAndStar";
		} else if (questionScopeVal == "wrong") {
			urlString = projectName + "/Word/howMuchWordOfGradeAndWrong";
		}
		$.ajax({
			url:urlString,
			type:"post",
			async:true,
			data:JSON.stringify(checkedGrade),
//			dataType:"json",
			contentType:"application/json",
			success:function (data) {
				$("#maxCountTs").text(data);
				var maxQuestionCountVal = $("#maxQuestionCount").val();
				if(data - maxQuestionCountVal < 0) {
					$("#maxQuestionCount").val(data);
				}
			}
		});
	});
	
	//判断输入题目数量是否合法
	$("#maxQuestionCount").on("change", function() {
		var maxQuestionCountVal = $("#maxQuestionCount").val();
		var maxCountTsText = $("#maxCountTs").text();
		var checkRule = /^[0-9]{0,}$/;
		if(checkRule.exec(maxQuestionCountVal)) {
			if(maxCountTsText - maxQuestionCountVal < 0) {
				$("#maxQuestionCount").val(maxCountTsText);
			}
		} else {
			$("#maxQuestionCount").val(maxCountTsText);
			alert("输入有违规字符");
		}
	})
});